package com.voltmao.controller.module.contract;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.contract.ProviderAuditServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 合同审核控制层
 *
 * @author zhanglin
 * @date 2018/04/10
 */
@Controller
@RequestMapping(value = "/module/contract/audit")
public class ProviderAuditController {

    @Autowired
    ProviderAuditServiceImpl providerAuditService;

    @ResponseBody
    @RequestMapping(value = "/apiInsertOrUpdate.json", method = RequestMethod.POST)
    public ApiResult insertOrUpdate(HttpServletRequest request){
        /*
         * 新增或修改合同审核和audit
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult) -> {
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            System.out.println(params.toString());
            try {
                providerAuditService.insertOrUpdateAudit(params);
                if(StringUtils.isNotEmpty(params.get("forDicShzt"))) {
                    if((params.get("forDicShzt")+"").equals("2")) {
                        providerAuditService.test(params.get("forContractProvider")+"");
                    }
                }
                apiResult.setStatus(Constants.SUCCESS);
            } catch (IllegalArgumentException e){
                e.printStackTrace();
                apiResult.status = Constants.UPDATE_FAIVAL.toString();
                apiResult.msg = "审核状态不合理";
            }
            catch (Exception e) {
                e.printStackTrace();
                apiResult.setStatus(Constants.PARAMETER_ERROR);
            }
            return null;
        });
    }

    @ResponseBody
    @RequestMapping("/apiDelete.json")
    public ApiResult deleteById(HttpServletRequest request){
        /*
         * 根据合同id删除相关的表
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult) -> {
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            try {
                providerAuditService.deleteAudit(params.get("type")+"",params.get("dataId")+"");
                apiResult.setStatus(Constants.SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                apiResult.setStatus(Constants.UPDATE_FAIVAL);
            }
            return null;
        });
    }

    @ResponseBody
    @RequestMapping(value = "/gridList.json", method = RequestMethod.POST)
    public GridViewResult testContractGrid(HttpServletRequest request) {
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/module/contract/audit/gridList.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                //抛出DATAURL异常
                return providerAuditService.selecAuditDataPage(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                return null;
            }
        });
    }


}
